package cn.ven.controller;

import cn.ven.entity.MyUser;
import cn.ven.entity.Tag;
import cn.ven.service.TagService;
import cn.ven.service.UserService;
import cn.ven.tool.RespJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/*
adm  管理员管理（A）  功能如下：
1./user/**   查找所有的用户，冻结用户，解冻用户，根据条件查找用户
2./tag/**   添加标签，修改标签，删除标签

 */

@Controller
@RequestMapping("/api/adm")
@CrossOrigin
public class AdmController {
    private Logger logger =LoggerFactory.getLogger(AdmController.class);
    @Autowired
    UserService userService;

    @Autowired
    TagService tagService;

    //  查找所有的用户
    @RequestMapping("/user/findAll")
    @ResponseBody
    public Map<String,Object> selectAllUser(){
        return RespJson.pack(200,"",userService.selectAllUser());
    }

    //根据closed 和role查找相关用户
    @RequestMapping("/user/selectUsers/{closed}/{role}")
    @ResponseBody
    public Map<String,Object> selectUsers(@PathVariable("closed")Integer closed,
                                          @PathVariable("role")String role){
        List<MyUser> users = userService.selectAllByClosedAndRole(closed, role);
        logger.info("users===="+users);
        return RespJson.pack(200,"",users);
    }

    //根据id删除用户
    @RequestMapping("/user/delById/{id}")
    @ResponseBody
    public Map<String,Object> delById(@PathVariable("id")Integer id){
        userService.delUser(id);
        return RespJson.pack(200,"删除成功","");
    }

    //冻结用户
    @RequestMapping("/user/closedUser/{id}")
    @ResponseBody
    public Map<String,Object> closedUser(@PathVariable("id")Integer id){
        MyUser user = userService.findById(id);
        user.setClosed(0);
        userService.updateUser(user);
        return RespJson.pack(200,"冻结成功","");
    }

    //解冻用户
    @RequestMapping("/user/openUser/{id}")
    @ResponseBody
    public Map<String,Object> openUser(@PathVariable("id")Integer id){
        MyUser user = userService.findById(id);
        user.setClosed(1);
        userService.updateUser(user);
        return RespJson.pack(200,"解冻成功","");
    }

    //添加类型
    @RequestMapping("/tag/addTag")
    @ResponseBody
    public Map<String,Object> addTag(@RequestBody Tag tag){
        tagService.addTag(tag);
        List<Tag> tags = tagService.findAll();
        return RespJson.pack(200,"添加成功",tags);
    }

    //修改类型
    @RequestMapping("/tag/updateTag")
    @ResponseBody
    public  Map<String,Object> updateTag(@RequestBody Tag tag){
        tagService.updateTag(tag);
        List<Tag> tags = tagService.findAll();
        return RespJson.pack(200,"更新成功",tags);
    }

    //删除类型
    @RequestMapping("/tag/delTag/{id}")
    @ResponseBody
    public Map<String,Object> delTag(@PathVariable("id")Integer id){
        tagService.delTag(id);
        List<Tag> tags = tagService.findAll();
        return RespJson.pack(200,"删除成功",tags);
    }



}
